//using UnityEngine;
using System;
using System.Collections;
using System.Threading;

namespace MxNetwork
{
    public class MxNwkObsListener
    {
        public MxNwkObsListener()
        {
            m_hListener = null;
        }

        public virtual bool OnListenFailed()
        {
            return true;
        }

        public MxNwkListener Handle
        {
            get
            {
                return m_hListener;
            }

            set
            {
                m_hListener = value;
            }
        }

        protected MxNwkListener m_hListener;
    }

    public class MxNwkObsConnecter
    {
        public MxNwkObsConnecter()
        {
            m_hConnecter = null;
        }

        public virtual bool OnConnectFailed()
        {
            return true;
        }

        public MxNwkConnecter Handle
        {
            get
            {
                return m_hConnecter;
            }

            set
            {
                m_hConnecter = value;
            }
        }

        protected MxNwkConnecter m_hConnecter;
    }

    public class MxNwkObsChannel
    {
        public virtual bool OnConnected(int hChannel, uint LocalType, uint LocalID, uint RemoteType, uint RemoteID, string IP)
        {
            Console.WriteLine("On connected, ChannelID:{0}, LocalType:{1}, LocalID:{2}, RemoteType:{3}, RemoteID:{4}, RemoteIP:{5}", hChannel, LocalType, LocalID, RemoteType, RemoteID, IP);
            return true;
        }

        public virtual bool OnDisConnected(int hChannel,int ErrorCode, uint LocalType, uint LocalID, uint RemoteType, uint RemoteID, string IP)
        {
            Console.WriteLine("On disConnected, ChannelID:{0}, LocalType:{1}, LocalID:{2}, RemoteType:{3}, RemoteID:{4}, RemoteIP:{5}", hChannel, LocalType, LocalID, RemoteType, RemoteID, IP);
            return true;
        }

        public virtual bool OnSendPacket(int hChl, MxNwkPacket pPkt)
        {
            return true;
        }

        static void MxAsynDispatchPacket(object o)
        {
            MxNwkPacket pPkt = (MxNwkPacket)o;
            switch ((MxNwkPacket.MxNwkPacketType)pPkt.GetPacketType())
		    {
		    case MxNwkPacket.MxNwkPacketType.NPT_MESSAGE:		
			    MxNetworkManager.GetInstance().SubmitMsg(pPkt);
			    break;
		    case MxNwkPacket.MxNwkPacketType.NPT_REQUEST:
			    MxNetworkManager.GetInstance().SubmitRequest(pPkt);
			    break;
            case MxNwkPacket.MxNwkPacketType.NPT_ACK:
			    MxNetworkManager.GetInstance().SubmitAck(pPkt);
			    break;
		    }		
        }


        public virtual bool OnRecvPacket(int hChl, MxNwkPacket pPkt)
        {
            MxNetworkManager.GetInstance().DispatchJob(new WaitCallback(MxAsynDispatchPacket), pPkt);
            return true;
        }
    }

    public class MxMsgObserver
    {
        public virtual void OnSubmit(MxNwkPacket pMsg)
        {

        }
    }

    public class MxRequestObserver
    {
        public virtual void OnSubmit(MxNwkPacket pRequest)
        {

        }
    }

    public class MxAckObserver
    {
        public virtual void OnSubmit(MxNwkPacket pRequest, MxNwkPacket pAck, MxToken pAct)
        {

        }
    }
}

